import { ElMessageBox } from 'element-plus'
import type { MessageBoxData } from 'element-plus'

export interface ConfirmOptions {
  title?: string
  message?: string
  confirmButtonText?: string
  cancelButtonText?: string
  type?: 'success' | 'info' | 'warning' | 'error'
  dangerouslyUseHTMLString?: boolean
}

export function useConfirm() {
  const confirm = async (
    message: string,
    options: ConfirmOptions = {}
  ): Promise<MessageBoxData> => {
    const {
      title = '提示',
      confirmButtonText = '确定',
      cancelButtonText = '取消',
      type = 'warning',
      dangerouslyUseHTMLString = false
    } = options

    return ElMessageBox.confirm(message, title, {
      confirmButtonText,
      cancelButtonText,
      type,
      dangerouslyUseHTMLString
    })
  }

  const confirmDelete = async (
    message: string = '确定删除该项吗？删除后无法恢复！',
    title: string = '删除确认'
  ): Promise<MessageBoxData> => {
    return confirm(message, {
      title,
      type: 'warning',
      confirmButtonText: '删除',
      cancelButtonText: '取消'
    })
  }

  const confirmAction = async (
    message: string,
    title: string = '操作确认',
    type: 'success' | 'info' | 'warning' | 'error' = 'info'
  ): Promise<MessageBoxData> => {
    return confirm(message, {
      title,
      type,
      confirmButtonText: '确定',
      cancelButtonText: '取消'
    })
  }

  return {
    confirm,
    confirmDelete,
    confirmAction
  }
}